package com.hongning.railway.ui.adapter;

import android.databinding.ViewDataBinding;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.hongning.railway.R;
import com.hongning.railway.databinding.ItemAnswerBinding;
import com.hongning.railway.databinding.ItemAnswerCheckBinding;
import com.hongning.railway.ui.main.fragments.college.exam.CheckAnswerFragmentViewModel;
import com.hongning.railway.ui.main.fragments.college.exam.ExamDetailFragmentViewModel;
import com.hongning.railway.ui.main.fragments.college.exam.itemviewmodel.AnswerCheckItemViewModel;
import com.hongning.railway.ui.main.fragments.college.exam.itemviewmodel.ExamItemViewModel;
import com.hongning.railway.util.SpannableStringUtils;

import java.util.List;

import me.goldze.mvvmhabit.base.AppManager;
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;

/**
 * Author: zhangjing
 * DATA:2019-08-10 16:41
 * DESC:
 * email:zhangjingworkmol@163.com
 */
public class AnswerCheckAdapter extends BindingRecyclerViewAdapter<AnswerCheckItemViewModel> {
    private RecyclerView recyclerView;
    private CheckAnswerFragmentViewModel viewModel;

    public void setViewModel(CheckAnswerFragmentViewModel viewModel) {
        this.viewModel = viewModel;
    }

    @Override
    public void onBindBinding(ViewDataBinding binding, int variableId, int layoutRes, int position, AnswerCheckItemViewModel item) {
        super.onBindBinding(binding, variableId, layoutRes, position, item);
        ItemAnswerCheckBinding answerBinding = (ItemAnswerCheckBinding) binding;
        if (position==0){
            answerBinding.btnPre.setEnabled(false);
        }else {
            answerBinding.btnPre.setEnabled(true);
        }
        if (position==getItemCount()-1){
            answerBinding.btnNext.setEnabled(false);
        }else {
            answerBinding.btnNext.setEnabled(true);
        }
        answerBinding.btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = position+1;
               recyclerView.smoothScrollToPosition(pos);
                viewModel.currentpos.set(pos+1+"/"+getItemCount()+"题");
            }
        });
        answerBinding.btnPre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = position-1;
                recyclerView.smoothScrollToPosition(pos);
                viewModel.currentpos.set(pos+1+"/"+getItemCount()+"题");
            }
        });
    }

    @Override
    public void setItems(@Nullable List<AnswerCheckItemViewModel> items) {
        super.setItems(items);
        viewModel.currentpos.set(1+"/"+items.size()+"题");
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }
}
